import requests
import time
import json
import urllib
import hashlib

baseurl = "http://zwtapi.dongben.cc"
phone = "15303867920"
token = "2c26b4d762f54287af87cb6463412a2f"
idfv = "24B8ABF5-A98F-4A8C-B065-C9AA273CD21D"
timeSpan = f"{time.time()}"
tomorrow = "Friday"#"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
sign = f"{timeSpan}{idfv}{tomorrow}"[::-1]
h1 = hashlib.md5()
h1.update(sign.encode(encoding='utf-8'))

header = {"token":token,
            "Accept-Language":"en-CN;q=1.0",
            "source_type":"apple-appstore",
            "Accept-Encoding":"gzip;q=1.0, compress;q=0.5",
            "uuid":idfv,
            "X-Plateform":"ios",
            "User-Agent":"zhengwutong/5.4.8 (com.rsaif.zwt; build:3; iOS 15.2.0) Alamofire/4.9.1",
            "version_code":"5.4.8",
            "Content-Type":"application/json",
            "push_device_type":"4",
            "lastLoginDevice":"iPhone 6s",
            "push_device_token":"65kzm687os56zuo",
            "lastLoginVersion":"iOS13.2.2",
            "lastLoginZwtVersion":"5.4.8",
            "authKey":"",
            "Accept": "*/*"
}
def login():
    dic = {"push_device_type":"4","lastLoginDevice":"iPhone 6s","password":"111111","mobile":"18134767785","push_device_token":"6182787ec298f3112be1b5084a79365a88e768150e5da2a112833d9f3f2f5fb0","lastLoginVersion":"iOS13.2.2"}
    data = bytes(json.dumps(dic), encoding='utf8')
    try:
        response = requests.post("http://zwtapi.dongben.cc/v2/Login/Login",data = data,headers=header)
        return json.loads(response.text)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT')
    else:
        print("error")
token = "27AC6985DE6BA3DDE91F5B0190DFC3A9"#print(login()['result']['token'])   
userid = 51353

def requestSMS():
    dic = {"mobile": phone,"timespan":timeSpan,"uuid":idfv,"sign":h1.hexdigest()}
    data = bytes(json.dumps(dic), encoding='utf8')
    # print(header)
    try:
        response = requests.post(baseurl+"/v2/Login/SmsVerifyCode",data = data,headers=header)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT',e);
    else:
        return superAdminLogin(mobile=phone)
        return json.loads(response.text)

def superAdminLogin(mobile=""):
    dic =  dict(account='cxj', password='tkgg@dev')
    # print(header)
    try:
        response = requests.post("http://zwtsuper.dongben.cc/Account/LoginForAdmin",data = dic)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT',e);
    else:
        return getSMSCode(response.cookies,mobile=mobile)
def getSMSCode(cookies,mobile=""):
    dic =  dict(page='1', rows='20', warning='0')
    # print(header)
    try:
        response = requests.post("http://zwtsuper.dongben.cc/Message/GetPageList",data = dic,cookies=cookies)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT',e);
    else:
        json1 = json.loads(response.text)
        if len(mobile) > 0:
            code = ''
            for item in json1['rows']:
                if item['mobile'] == mobile and item['content'].find("请输入验证码：") >= 0:
                    code = item['content'][7:11]
                    break
            return loginWithSMS(code)
        else:
            return json1
def loginWithSMS(code):
    dic = {"mobile": phone,
        "code":code,
        "push_device_type":"4",
        "push_device_token": "65kzm687os56zuo",
        "lastLoginDevice": "iPhone 6s",
        "lastLoginVersion": "iOS13.2.2"}
    data = bytes(json.dumps(dic), encoding='utf8')
    # print(header)
    try:
        response = requests.post(baseurl+"/v2/Login/LoginByCode",data = data,headers=header)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT',e);
    else:
        return json.loads(response.text)

def requestArea():
    dic = {"mobile":"15036379106"}
    # print(header)
    try:
        response = requests.get(baseurl+"/v2/Login/GetArea",params = dic,headers=header)
        print(response.url)
    except urllib.error.URLError as e:
        # if isinstance(e.reason, socket.timeout):
        print('TIME OUT',e);
    else:
        return json.loads(response.text)
if __name__ == "__main__":
    # print(login())
    # print(requestSMS())
    # login()
    # print(loginWithSMS("5443"))
    # print(requestArea())
    # print(superAdminLogin())
    print(requestSMS())